using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Fated.SaveEngine;
using Fated.Character.ChartGeneration;

namespace Fated.Character {
    internal class PlayerCharacter : AbstractCharacter {
        #region Member Keys
        private const string GoldKey = "gold";
        private const string AlliesKey = "allies";
        #endregion

        public PlayerCharacter()
            : base() {
                Allies = new AllyCollection();
        }
        public PlayerCharacter(DateTime d) : this() {
            CharChart.setBirthdate(d);
            Allies = new AllyCollection();
        }
        public PlayerCharacter(Dictionary<string, object> input) {
            setData(input);
        }

        public int addGold(int amount) {
            Gold += amount;
            return Gold;
        }

        public void addAlly(AllyData newAlly) {
            Allies.addAlly(newAlly);
        }
        public void removeAlly(String name) {
            Allies.removeAlly(name);
        }
        public AllyData getAlly(String name) {
            return Allies.getAlly(name);
        }
        public AllyData getAlly(int index) {
            return Allies.getAlly(index);
        }

        public bool getAttribute<ofType>(string keyName, ref ofType attribOut) {
            if (data.ContainsKey(keyName) && data[keyName].GetType() == typeof(ofType)) {
                attribOut = (ofType)data[keyName];
                return true;
            } else {    //Handle error
                attribOut = default(ofType);
                return false;
            }
        }
        public void setAttribute(string keyName, object attribIn) {
            if (data.ContainsKey(keyName)) {
                data[keyName] = attribIn;
            } else {    //Handle error
                data.Add(keyName, attribIn);
            }
        }

        public override void setData(Dictionary<string, object> input) {
            base.setData(input);
            Dictionary<string, object> alliesData = (Dictionary<string, object>)input[AlliesKey];
            Allies = new AllyCollection(alliesData);
        }

        public string printStats() {
            string output = "Health: " + MaxHealth + "\r\n" +
                "Magic: " + MaxMagic + "\r\n" +
                "Body: " + Body + "\r\n" +
                "Mind: " + Mind + "\r\n" +
                "Spirit: " + Spirit + "\r\n" +
                "Social: " + Social + "\r\n" +
                "Speed: " + Speed + "\r\n";
            return output;
        }
        private int Gold {
            get {
                if (data.ContainsKey(GoldKey)) {
                    object o = data[GoldKey];
                    String ooo = o.ToString();
                    //Double number = (Double)o;
                    return int.Parse(ooo);
                } else
                    data.Add(GoldKey, 0.0);
                return 0;
            }
            set {
                if (data.ContainsKey(GoldKey))
                    data[GoldKey] = value;
                else
                    data.Add(GoldKey, value);
            }
        }
        internal AllyCollection Allies {
            get {
                if (data.ContainsKey(AlliesKey)) {
                    return (AllyCollection)data[AlliesKey];
                }
                throw new Exception("Allies key not found.");
            }
            set {
                if (data.ContainsKey(AlliesKey))
                    data[AlliesKey] = value;
                else
                    data.Add(AlliesKey, value);
            }
        }
        
    }
}
